/**
 * @author 黄波
 * 创建日期：2011Oct 8, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.util.ArrayList;
import java.util.List;

import com.platform.os.util.Util;
import com.platform.pojo.ResInfo;
import com.platform.pojo.ResType;
import com.platform.pojo.UserInfo;
import com.platform.res.bean.ResInfoBean;
import com.platform.res.bo.ResBo;
import com.platform.user.bo.UserBo;

/**
 * 加載資源項
 */
public class LoadResItemAction extends NJCCSuport {

	private String id;	//資源ID
	private ResType type;	//資源所在類別
	private ResInfoBean info;	//资源信息
	private Long reviewCount;	//资源被评论次数

	private List<ResType> alltypes = new ArrayList<ResType>();	//所有资源类型列表

	private List<ResType> types = new ArrayList<ResType>();	//一级资源类型列表
	private List<ResType> typelist1 = new ArrayList<ResType>();	//资源子类型列表1
	private List<ResType> typelist2 = new ArrayList<ResType>();	//资源子类型列表2
	private List<ResType> typelist3 = new ArrayList<ResType>();	//资源子类型列表3
	private List<ResType> typelist4 = new ArrayList<ResType>();	//资源子类型列表4
	private List<ResType> typelist5 = new ArrayList<ResType>();	//资源子类型列表5
	
	
	@Override
	public String execute() throws Exception {
		if(id == null || id.equals("")) {
			return ERROR;
		}

		ResBo resBo = ResBo.getInstance();
		UserBo userBo = UserBo.getInstance();
		//获得一级资源类型列表
		alltypes = resBo.getTypes();
		types = resBo.getTypes("0");

		//查询二级资源类型列表
		if(types != null) {
			if(types.size() > 0 && types.get(0) != null) {
				typelist1 = resBo.getTypes(types.get(0).getId().toString());
			}
			if(types.size() > 1 && types.get(1) != null) {
				typelist2 = resBo.getTypes(types.get(1).getId().toString());
			}
			if(types.size() > 2 && types.get(2) != null) {
				typelist3 = resBo.getTypes(types.get(2).getId().toString());
			}
			if(types.size() > 3 && types.get(3) != null) {
				typelist4 = resBo.getTypes(types.get(3).getId().toString());
			}
			if(types.size() > 4 && types.get(4) != null) {
				typelist5 = resBo.getTypes(types.get(4).getId().toString());
			}
		}
		//根据ID查询资源信息
		ResInfo item = resBo.getInfo(id);
		info = new ResInfoBean();
		info.setInfo(item);
		if(alltypes != null) {
			ResType tmp = getTypeName(item.getTypeId(), alltypes);
			if(tmp != null) {
				info.setTypeName(tmp.getName());
			} else {
				info.setTypeName("课程资源");
			}
		}
		UserInfo user = userBo.getUser(item.getUploadUserId().toString());
		if(user != null) {
			info.setUserName(user.getLoginName());
		} else {
			info.setUserName(null);
		}
//		bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
		info.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));

		type = getTypeName(info.getInfo().getTypeId(), alltypes);
		reviewCount = resBo.getReviewCount(info.getInfo().getId().toString());
		return SUCCESS;
	}
	

	/**
	 * @param typeId
	 * @return
	 */
	private ResType getTypeName(Integer typeId, List<ResType> types) {
		if(typeId == null) {
			return null;
		}
		for(ResType item : types) {
			if(item.getId() == typeId) {
				return item;
			}
		}
		return null;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<ResType> getAlltypes() {
		return alltypes;
	}
	public void setAlltypes(List<ResType> alltypes) {
		this.alltypes = alltypes;
	}
	public List<ResType> getTypes() {
		return types;
	}
	public void setTypes(List<ResType> types) {
		this.types = types;
	}
	public List<ResType> getTypelist1() {
		return typelist1;
	}
	public void setTypelist1(List<ResType> typelist1) {
		this.typelist1 = typelist1;
	}
	public List<ResType> getTypelist2() {
		return typelist2;
	}
	public void setTypelist2(List<ResType> typelist2) {
		this.typelist2 = typelist2;
	}
	public List<ResType> getTypelist3() {
		return typelist3;
	}
	public void setTypelist3(List<ResType> typelist3) {
		this.typelist3 = typelist3;
	}
	public List<ResType> getTypelist4() {
		return typelist4;
	}
	public void setTypelist4(List<ResType> typelist4) {
		this.typelist4 = typelist4;
	}
	public List<ResType> getTypelist5() {
		return typelist5;
	}
	public void setTypelist5(List<ResType> typelist5) {
		this.typelist5 = typelist5;
	}
	public ResType getType() {
		return type;
	}
	public void setType(ResType type) {
		this.type = type;
	}
	public ResInfoBean getInfo() {
		return info;
	}
	public void setInfo(ResInfoBean info) {
		this.info = info;
	}


	public Long getReviewCount() {
		return reviewCount;
	}


	public void setReviewCount(Long reviewCount) {
		this.reviewCount = reviewCount;
	}

}
